package architecture.demo13;

import java.util.ArrayList;

public class ContactList extends ArrayList<Contact> {

    public void addContact(Contact contact) {
        add(contact);
        System.out.println("添加联系人：" + contact.getName());
    }

    public void searchContacts(String keyword) {
        System.out.println("搜索联系人包含关键词：" + keyword);
        boolean found = false;
        for (Contact contact : this) {
            if (contact.getName().contains(keyword)) {
                System.out.println(contact);
                found = true;
            }
        }
        if (!found) {
            System.out.println("没有找到联系人");
        }
    }

    public void removeContact(Contact contact) {
        boolean removed = remove(contact);
        if (removed) {
            System.out.println("删除联系人：" + contact.getName());
        } else {
            System.out.println("联系人不存在：" + contact.getName());
        }
    }


    public void updateContact(Contact oldContact, Contact newContact) {
        int index = indexOf(oldContact);
        if (index != -1) {
            set(index, newContact);
            System.out.println("更新联系人：" + oldContact.getName());
        }
    }
}


